package com.thinksns.sociax.t4.android.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.AdapterView;

import com.thinksns.sociax.android.R;
import com.thinksns.sociax.api.Api;
import com.thinksns.sociax.t4.adapter.AdapterUserFollowingListNew;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.data.StaticInApp;
import com.thinksns.sociax.t4.android.user.ActivityUserInfo_2;
import com.thinksns.sociax.t4.android.video.ToastUtils;
import com.thinksns.sociax.t4.model.ModelSearchUser;
import com.thinksns.sociax.t4.model.ModelUser;
import com.thinksns.sociax.thinksnsbase.activity.widget.EmptyLayout;
import com.thinksns.sociax.thinksnsbase.base.BaseListFragment;
import com.thinksns.sociax.thinksnsbase.base.BaseListPresenter;
import com.thinksns.sociax.thinksnsbase.base.ListBaseAdapter;
import com.thinksns.sociax.thinksnsbase.bean.ListData;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.Serializable;

/**
 * 类说明： 找人一级目录
 * 
 * @author wz
 * @date 2014-11-4
 * @version 1.0
 */
public class FragmentFindPeople extends BaseListFragment<ModelSearchUser> {
	private BroadcastReceiver mUpdateFollowRecevier;

	@Override
	protected boolean requestDataIfViewCreated() {
		return false;
	}

	@Override
	protected void setRefreshMode(int mode) {
		super.setRefreshMode(BaseListFragment.DISABLED);
	}


	@Override
	protected ListBaseAdapter<ModelSearchUser> getListAdapter() {
		return new AdapterUserFollowingListNew(getActivity()) {
			@Override
			public int getMaxId() {
				return (mDatas == null || mDatas.size() == 0)? 0 :
						((ModelSearchUser)mDatas.get(mDatas.size() -1)).getUid();
			}

			@Override
			protected boolean hasFooterView() {
				return false;
			}
		};
	}

	@Override
	protected void initListViewAttrs() {
		super.initListViewAttrs();
	}

	@Override
	protected void initPresenter() {
		mPresenter = new BaseListPresenter<ModelSearchUser>(getActivity(), this) {

			@Override
			public ListData<ModelSearchUser> parseList(String result) {
				try {


					ListData<ModelSearchUser> listData = new ListData<ModelSearchUser>();
					if(result.startsWith("{") && result.endsWith("}")) {
						//数据异常
						return listData;
					}

					JSONArray jsonArray = new JSONArray(result);
					for (int i = 0; i < jsonArray.length(); i++) {
						try {
							ModelSearchUser follow = new ModelSearchUser(
									jsonArray.getJSONObject(i));
							listData.add(follow);
						} catch (JSONException e) {
							e.printStackTrace();
						}

					}
					return listData;
				}catch(JSONException e) {
					e.printStackTrace();
				}

				return null;
			}

			@Override
			protected ListData<ModelSearchUser> readList(Serializable seri) {
				return (ListData<ModelSearchUser>)seri;
			}

			@Override
			public String getCachePrefix() {
				return "user_list";
			}

			@Override
			public void loadNetData() {
				new Api.Users().searchUser(mHandler, 8);
			}
		};

		mPresenter.setCacheKey("find_people");
	}

	@Override
	protected void initReceiver() {
		super.initReceiver();
		mUpdateFollowRecevier = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				String action = intent.getAction();
				// 对某用户关注/取消关注后触发的
				if (action.equals(StaticInApp.NOTIFY_FOLLOW_USER)) {
					int uid = intent.getIntExtra("uid", 0);
					int follow = intent.getIntExtra("follow", 0);
					if (uid > 0) {
						updateUserFollow(uid, follow);
					}
				}
			}
		};

		IntentFilter intentFilter = new IntentFilter(StaticInApp.NOTIFY_FOLLOW_USER);
		if (intentFilter != null) {
			getActivity().registerReceiver(mUpdateFollowRecevier, intentFilter);
		}
	}

	@Override
	public void initData() {
		mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
		mPresenter.loadNetData();
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		getActivity().unregisterReceiver(mUpdateFollowRecevier);
	}

	//请求换一换数据
	public void requestData() {
		//主动刷新，优先从网络获取数据
		mPresenter.loadInitData(true);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		ModelSearchUser user = mAdapter.getItem((int)id);
		if(user != null) {
			Intent intent = new Intent(getActivity(), ActivityUserInfo_2.class);
			//4.5增加能否进入个人中心的判断
			if (Thinksns.getMy().getUid() != user.getUid() && ModelUser.CAN_NOT_TO_HOME == user.getSpace_privacy()) {
				ToastUtils.showToast(R.string.tip_no_oauth_to_other_home);
			} else {
				intent.putExtra("uid", user.getUid());
				startActivity(intent);
			}
		}
	}

	@Override
	public void onLoadDataSuccess(ListData<ModelSearchUser> data) {
		mEmptyLayout.setErrorImag(R.drawable.ic_no_yh);
		mEmptyLayout.setNoDataContent(getResources().getString(R.string.empty_user));
		super.onLoadDataSuccess(data);
	}

	@Override
	public void onLoadComplete() {
		super.onLoadComplete();
	}

	/**
	 * 刷新关注、取消关注用户的状态
	 *
	 * @param following
	 */
	private void updateUserFollow(int uid, final int following) {
		for (int i = 0; i < mAdapter.getDataSize(); i++) {
			if (mAdapter.getItem(i).getUid() == uid) {
				mAdapter.getItem(i).setFollowing(String.valueOf(following));
				break;
			}
		}

		mAdapter.notifyDataSetChanged();
	}
}
